package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.DeptClientApi;
import com.zhentao.dto.Dto;
import com.zhentao.pojo.Emp;
import com.zhentao.pojo.EmpSer;
import com.zhentao.service.EmpService;
import com.zhentao.mapper.EmpMapper;
import com.zhentao.utils.Result;
import jdk.nashorn.internal.ir.IfNode;
import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* @author 86191
* @description 针对表【tb_emp】的数据库操作Service实现
* @createDate 2025-04-27 09:51:10
*/
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp>
    implements EmpService{
    @Autowired
    private EmpMapper empMapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Autowired
    private DeptClientApi deptClientApi;
    @Autowired
    private RestHighLevelClient client;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public String addEmp(Emp emp) {
        redisTemplate.opsForList().leftPushAll("current:emp:",emp);
        int insert = empMapper.insert(a());
        String s="";
        if (insert==1){
            deptClientApi.addNum(emp.getGradeId());
            s="添加成功";
        }
        return s;
    }

    @Override
    public String updStatus(Dto dto) {
        kafkaTemplate.send("topic1", JSON.toJSONString(dto));
        return "状态修改成功";
    }

    @Override
    public Result findAll() throws IOException {
        //查询所有
        List<Emp> emps = empMapper.selectList(null);
        BulkRequest bulkRequest=new BulkRequest("emp");
        for (Emp e:emps){
            Emp emp=new Emp();
            BeanUtils.copyProperties(e,emp);
            String json = JSON.toJSONString(emp);
            IndexRequest indexRequest=new IndexRequest()
                    .id(emp.getEid()+"")
                    .source(json, XContentType.JSON);
            bulkRequest.add(indexRequest);
        }
        client.bulk(bulkRequest, RequestOptions.DEFAULT);
        //从es中取出来
        SearchRequest searchRequest=new SearchRequest("ingredient");
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        JSONObject jsonObject = JSON.parseObject(search.toString());

        //添加历史记录到mongodb
        EmpSer empSer=new EmpSer();
        empSer.setCreateTime(new Date());
        Query query=new Query();
        List<EmpSer> empSers = mongoTemplate.find(query, EmpSer.class);
        if (empSers.size()<10){
            mongoTemplate.save(empSer);
        }else {
            return Result.ERROR("mongodb记录超过10",null);
        }

        return Result.OK("从es查询",jsonObject);
    }

    @Override
    public List<EmpSer> findSer() {
        Query query=new Query();
        List<EmpSer> empSers = mongoTemplate.find(query, EmpSer.class);
        return empSers;
    }

    @Override
    public String delSer(String id) {
        Query query=new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query, EmpSer.class);
        return "删除成功";
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public Emp a(){
        List<Emp> range = redisTemplate.opsForList().range("current:emp:", 0, -1);
        Emp emp=new Emp();
        for (Emp r:range){
            emp=r;
        }
        System.err.println(emp);
        return emp;
    }
}




